<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../../lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="../../css/public.css" media="all">
    <style>
        body {
            background-color: #ffffff;
        }
    </style>
</head>

<body>
    <div class="layui-form layuimini-form">
        <div class="layui-form-item">
            <label class="layui-form-label required">所属学校</label>
            <div class="layui-input-inline">
                <select name="id" id="school" lay-filter="id">
                    <!-- <option value="">请选择学校</option> -->
                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label required">项目名称</label>
            <div class="layui-input-block">
                <input type="text" name="name" lay-verify="required" lay-reqtext="不能为空" placeholder="请输入项目名" value=""
                    id="reg" class="layui-input">
                <tip>填写项目名称。</tip>
            </div>
        </div>

        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn layui-btn-normal" lay-submit lay-filter="saveBtn">确认保存</button>
            </div>
        </div>
    </div>
    <script src="../../lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
    <script src="../../js/lay-config.js?v=2.0.0" charset="utf-8"></script>
    <script>
        layui.use(['form', 'laydate', 'cookie'], function () {
            var form = layui.form,
                layer = layui.layer,
                $ = layui.$;
            $(function () {
                $.ajax({
                    url: "/club/school",
                    method: "GET",
                    contentType: "application/json",
                    success: function (data) {
                        var data = data.data;
                        $('#school').empty();
                        $('#school').append(new Option("请选择学校", ''));
                        if ($.cookie("role") == 0) {
                            var str = $.cookie("schools").split(',');
                            for (var i = 0; i < str.length; i++) {
                                for (var j = 0; j < data.length; j++) {
                                    if (str[i] == data[j].s_name) {
                                        $('#school').append(new Option(data[j].s_name, data[
                                            j].id));
                                        break;
                                    }
                                }
                            }
                        } else {
                            $.each(data, function (index, value) {
                                // 前者是页面显示的值，后者是传递给后台的值。
                                $('#school').append(new Option(value.s_name, value
                                    .id)); // 下拉菜单里添加元素
                            });
                        }
                        //渲染select
                        form.render('select');
                    },
                    error: function (err) {
                        layer.msg("服务器错误，请稍后重试!", {
                            icon: 2
                        });
                    }
                })
            })

            //监听提交
            form.on('submit(saveBtn)', function (data) {
                var json = JSON.stringify(data.field);
                $.ajax({
                    url: "/club",
                    method: "POST",
                    data: json,
                    contentType: "application/json",
                    success: function (data) {
                        if (data.code === 200) {
                            layer.msg("添加成功！", {
                                icon: 1
                            });
                            setTimeout(function () {
                                var iframeIndex = parent.layer
                                    .getFrameIndex(window.name);
                                parent.layer.close(iframeIndex);
                                parent.location.href =
                                    '/page/table-club.html';
                            }, 1000)
                        } else {
                            layer.msg("该校已存在该项目，不可重复添加!", {
                                icon: 2
                            });
                            return $("#reg").val('');
                        }

                    },
                    error: function (err) {
                        layer.msg("服务器错误，请稍后重试!", {
                            icon: 2
                        });
                    }
                })
                return false;
            });
        });
    </script>
</body>

</html>